package com.qingke.memo.command.system;

import com.qingke.memo.command.SystemCommand;
import com.qingke.memo.pojo.CustomTag;
import com.qingke.memo.unit.MConsole;

public class AlterCustomTag extends SystemCommand {

	@Override
	public void execute() {
		CustomTag customTag=null;
		while(true){
			String str=MConsole.promptNotEmpty("choose select CustomTag by id or code (id,code):");
			if("id".equalsIgnoreCase(str))
				customTag=byId();
			else if("code".equalsIgnoreCase(str))
				customTag=byCode();
			else{
				MConsole.println("err input");
				continue;
			}
			if(customTag==null)
				return ;
			break;
		}
		while(true){
			MConsole.println("alter (1.code 2.name 3.description 4.confirm alter)");
			String str=MConsole.promptNotEmpty("alter custom tag>");
			if("1".equalsIgnoreCase(str))
				code(customTag);
			else if("2".equalsIgnoreCase(str))
				name(customTag);
			else if("3".equalsIgnoreCase(str))
				description(customTag);
			else if("4".equalsIgnoreCase(str))
				break;
			else 
				MConsole.println("err input");
		}
		
		boolean isSuccess=dao.updateCustomTag(customTag);
		if(isSuccess)
			MConsole.println("update custom tag success.");
		else
			MConsole.println("update custom tag failed.");
	}

	private CustomTag byId(){
		int id=MConsole.getInt("custom tag id:");
		if(!dao.isCustomTagExistById(id)){
			MConsole.println("the custom tag is not existed.");
			return null;
		}
		return dao.selectCustomTagById(id);
	}
	
	private CustomTag byCode(){
		String code=MConsole.promptNotEmpty("custom tag code:");
		if(!dao.isCustomTagExistByCode(code)){
			MConsole.println("the custom tag is not existed.");
			return null;
		}
		return dao.selectCustomTagByCode(code);
	}
	
	private void code(CustomTag customTag){
		String code=MConsole.promptNotEmpty("new custom tag code:");
		if(dao.isCustomTagExistByCode(code)){
			MConsole.println("the custom tag code have been existed.");
			return ;
		}
		customTag.setCode(code);
	}
	
	private void name(CustomTag customTag){
		String name=MConsole.promptNotEmpty("new custom tag name:");
		customTag.setName(name);
	}
	
	private void description(CustomTag customTag){
		String description=MConsole.promptNotEmpty("new custom tag description:");
		customTag.setDescription(description);
	}
	
}
